<?php

// 订单管理首页
class OrderAction extends Action
{

    public function _initialize()
    {
        checkLoginSession();
    }

    public function index()
    {//分页显示所有订单
        $ordDao = D("Order");
        $tname = 'Order';
        $Field = 'id_tb_Order';
        $Asc = ' DESC';
        $searchwords = isset($_POST['status'])?$_POST['status']:' 1=1 ';        
        $arrayVa = ShowTable($tname, $Field, $Asc, $searchwords);
        $ordList = $arrayVa['a'];
        $page = $arrayVa['b'];
        if (count($arrayVa['a']) < 1 or ($arrayVa['a'] == null))
        {
            $this->assign('empty', '没有记录');
        }

        //$this->assign('content', 'Order:index');
        $this->assign('orderPreList', $ordList);
        $this->assign('page', $page);        
        $this->display('Order:index');
    }


    //处理提交的订单，获得订单一个订单流水号，session存储该订单
    public function addOrder()
    {   
        $serial_number = date('YmdGis');
        $userid = $_SESSION['UserInfo']['boss'];    
        $total_value = 0;//实际金额
        $total_weight = 0;
        $max_value = 0;//最大金额
        
        $Order = D('Order');
        $dptPdtPDO = D('Depot_product');
        $OrdDetalPdo = D("Order_detail");
        $ptPdo = D('Product');
        
        $str = $_POST["data"]; //编码，数量
        $datai = operOrdRec($str);
        $x = count($datai);
        $y = 0;        
    if($x>0)
        {//存在明细记录  
                while ($y < $x)
                {   
                    $ppcode = $datai[$y]['simple_code'];
                    $ppcount = $datai[$y]['count'];
                    $ptr = $ptPdo->where(" simple_code = '$ppcode' and userid = '$userid'")->find();
                    
                    $datai[$y]['mprice'] = (float)($ptr['price'])*$ppcount;
                    $datai[$y]['tprice'] = (float)($datai[$y]['actual_price']);
                    $datai[$y]['tweight'] = (int)($ptr['weight'])*$ppcount;

                    $total_value = $total_value+ $datai[$y]['tprice'];
                    $max_value = $max_value+ $datai[$y]['mprice'];
                    $total_weight =$total_weight + (int)($datai[$y]['tweight']); 
                                                     
                    $datai[$y]['serial_number'] = $serial_number;
                    $y = $y + 1;
                }
                
                 //添加主订单
                    $data = array("serial_number" => $serial_number, "express" => $_POST['express'],
                    "cust_info" => $_POST['cust_info'], "userid" => $userid,
                    "conductor_id" => $_SESSION['UserInfo']['user_id'], "date" => date('Y-m-d'),
                    "cust_note" => $_POST['cust_note'], "total_value" => $total_value,
                    "total_weight" => $total_weight,"from" => $_POST['from'],"max_value" =>$max_value);                
                    if($topicid = $Order->add($data))
                    {
                     
                    }
                    else
                    {
                        echo ajaxrt('error','添加订单记录失败');
                        Session::set('nowOrdRec', null);EXIT();
                    }                
                $y=0;
                while ($y < $x)
                {
                   $datai[$y]['id_tb_order'] = $topicid;
                    if ($id_tb_order_detail = $OrdDetalPdo->add($datai[$y]))
                    {
                        $ctt = $datai[$y]['count'];
                        $spcode = $datai[$y]['simple_code'];
                        $strsql = "UPDATE `tb_depot_product` SET `lock_sale_count` = lock_sale_count + $ctt where simple_code = '$spcode'  and userid = '$userid' ";
                        if($rttt = $dptPdtPDO->execute($strsql))
                        {
                        Session::set('nowOrdRec', $data);
                        Session::set('nowOrdRecsCount', count($datai));
                        Session::set('nowOrdRec' . $y, $datai[$y]);                        
                        }
                        else
                        {
                            echo ajaxrt('error','锁定库存失败'.$strsql);EXIT();
                        }
                    } 
                    else
                    {
                      echo ajaxrt('error','更新订单详单记录失败');EXIT();
                    } 
                echo ajaxrt('ok','下单成功，库存锁定成功',$serial_number);EXIT();                         
                    
                }
                

        }//如果有明细记录  
    }



    public function getDetailRec()
    {

        $ODetal = D('Order_detail');
		$userid = $_SESSION['UserInfo']['boss'];
        $or_id = $_POST['or_id'];
        $data = $ODetal->where('id_tb_order = ' . "'" . $or_id . "'")->
            findAll();
        $ct = count($data);
        if ($ct < 1)
        {
        $status = 'ok';
        echo ajaxrt('ok','该订单记录不存在详单',$data);            
        } 
        else
        {
        echo ajaxrt('ok','成功获得详单信息',$data);
        }
    }

    public function ShowOrderPage()
    {
        $sn = $_GET['sn'];
        $inRecFnum = '配货单(订单)流水号：' . $sn;
        //$id_order = $_GET['id'];
        Vendor('Tcpdf.tcpdf');
        $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

        $pdf->SetCreator(PDF_CREATOR);
        $pdf->SetAuthor('美工衣橱');
        $pdf->SetTitle('配货单(订单)');
        $pdf->SetSubject('流水号： ' . $_GET['sn']);
        $pdf->SetKeywords('美工衣橱, 配货单(订单)');

        $pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, $inRecFnum,
            PDF_HEADER_STRING);
        $pdf->setHeaderFont(array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
        $pdf->setFooterFont(array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
        $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);

        $pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
        $pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
        $pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
        $pdf->SetAutoPageBreak(true, PDF_MARGIN_BOTTOM);
        $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
        $pdf->SetFont('droidsansfallback', '', 12);

        //$pdf->AddPage('B5');
        $pdf->AddPage();

        $userid = $_SESSION['UserInfo']['boss'];
        $sn = $_GET['sn'];
        $OdDetal = D('Order_all_info');
        $recs = $OdDetal->where("serial_number =  '$sn'  AND userid = '$userid' " )->findAll();
        if (($ct = count($recs)) > 0)
        {
            $i = 0;
            $htmlstr = '<br/>详单记录共' . $ct . '个<br/><table>';
            while ($i < $ct)
            {   $lcc_code = getLcode($recs[$i]['simple_code'],$recs[$i]['count']); 
                $htmlstr = $htmlstr . '<tr>';
                $htmlstr = $htmlstr . '<td>序号：' . ($i + 1) . '</td>';
                $htmlstr = $htmlstr . '<td>商品简码：' . $recs[$i]['simple_code'] . '</td>';
                $htmlstr = $htmlstr . '<td>数量：' . $recs[$i]['count'] . '</td>';
                $htmlstr = $htmlstr . '<td>位置码：' . $lcc_code . '</td>';
                $htmlstr = $htmlstr . '</tr>';
                $i = $i + 1;
            }
            $kind = 'hand';
            $baseinfo = makeOdBaseInfo($kind, $recs[0]); //订单基本信息数组,kind是手工还是从在线订单获取
            $htmlstr = $baseinfo . $htmlstr . '</table><br/>出库签字确认:_____________';

            $pdf->writeHTML($htmlstr, true, false, true, false, '');
            $pdf->Output('Order.pdf', 'I');
        } 
        else
        {
            $this->error('订单序列号不存在对应订单记录');
        }
    }

public function SyscTaoOrder(){//同步淘宝订单
    
    echo "Todo...";
    
    
    
}

    
}

?>